package aboutTime;
/*
*运动场上有很多组照明设备，下面A集合中，描述出了不同照明设备的启动（begintime）停止（endtime）时间，
照明设备的id已经处理掉了无需考虑，仔细看这些时间段可以发现，这些时间有重合（表示这段时间多组照明同时亮）、有空挡（表示这段时间一组照明都没有开）。
请通过java代码实现，获得此足球场上有照明设备开启的时间段集合（求A集合在时间维度上的并集）。收起

A集合：
[
{begintime:2020-01-01 00:04:00,endtime:2020-01-01 00:06:00},
{begintime:2020-01-01 00:01:05,endtime:2020-01-01 00:03:15},
{begintime:2020-01-01 00:09:00,endtime:2020-01-01 00:10:00},
{begintime:2020-01-01 00:07:35,endtime:2020-01-01 00:08:40},
{begintime:2020-01-01 00:12:00,endtime:2020-01-01 00:17:00},
{begintime:2020-01-01 00:12:05,endtime:2020-01-01 00:13:10},
{begintime:2020-01-01 00:19:00,endtime:2020-01-01 00:21:00},
{begintime:2020-01-01 00:15:20,endtime:2020-01-01 00:20:45}
]
﻿
期望得到的输出结果：
[
{begintime:2020-01-01 00:01:05,endtime:2020-01-01 00:03:15},
{begintime:2020-01-01 00:04:00,endtime:2020-01-01 00:06:00},
{begintime:2020-01-01 00:07:35,endtime:2020-01-01 00:08:40},
{begintime:2020-01-01 00:09:00,endtime:2020-01-01 00:10:00},
{begintime:2020-01-01 00:12:00,endtime:2020-01-01 00:21:00}
]
*/

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.*;

/**
 * 有错误,待更正,
 * (另参考答案在Demo420answer)
 */
public class Demo0420 {
    public static void main(String[] args) throws ParseException {
        List<Map<String, String>> alist = new ArrayList<Map<String, String>>() {{
                add(new HashMap<String, String>() {{
                    put("begintime", "2020-01-01 00:04:00");
                    put("endtime","2020-01-01 00:06:00");
                }});
                add(new HashMap<String, String>() {{
                    put("begintime", "2020-01-01 00:01:05");
                    put("endtime","2020-01-01 00:03:15");
                }});
                add(new HashMap<String, String>() {{
                    put("begintime", "2020-01-01 00:09:00");
                    put("endtime","2020-01-01 00:10:00");
                }});
                add(new HashMap<String, String>() {{
                    put("begintime", "2020-01-01 00:07:35");
                    put("endtime","2020-01-01 00:08:40");
                }});
                add(new HashMap<String, String>() {{
                    put("begintime", "2020-01-01 00:12:00");
                    put("endtime","2020-01-01 00:17:00");
                }});
                add(new HashMap<String, String>() {{
                    put("begintime", "2020-01-01 00:12:05");
                    put("endtime","2020-01-01 00:13:10");
                }});
                add(new HashMap<String, String>() {{
                    put("begintime", "2020-01-01 00:19:00");
                    put("endtime","2020-01-01 00:21:00");
                }});
                add(new HashMap<String, String>() {{
                    put("begintime", "2020-01-01 00:15:20");
                    put("endtime","2020-01-01 00:20:45");
                }});
            }};

        List<Map<String, String>> newList=new ArrayList<Map<String, String>>();

        for (Map<String, String> map : alist) {
            Date begintime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(map.get("begintime"));
            Date endtime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(map.get("endtime"));
            if(newList.size()==0){
                newList.add(map);
            }
            int count=newList.size();
            for (int i = 1; i <=count ; i++) {
                Map<String, String> newMap=newList.get(i);
                Date checkBegintime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(newMap.get("begintime"));
                Date checkEndtime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(newMap.get("endtime"));

                boolean isCover=!(begintime.after(checkEndtime) || endtime.before(checkBegintime));
                if(isCover){
                    String newBegintime=begintime.after(checkBegintime)?newMap.get("begintime"):map.get("begintime");
                    String newEndtime=endtime.before(checkEndtime)?newMap.get("endtime"):map.get("endtime");
                    newMap.put("begintime",newBegintime);
                    newMap.put("endtime",newEndtime);
                    continue;
                }
                newList.add(map);
            }
        }
    }
}
